#!/usr/bin/env bash

# download-flite-voices.sh
#
# by adnan360 | License: CC0
#
# A quick and dirty script to download all the INDIC and US voices for
# Flight TTS.
#
# This has been created to be used where the "get_voices" binary is not
# available.
#
# Usage:
# - Create a new directory to keep voice files
# - cd into it
# - Save this inside the directory
# - Then run "bash download-flite-voices.sh"

# Files to look for voice file list
declare -a sources
sources+=("http://www.festvox.org/flite/packed/flite-2.1/voices/indic_voices")
sources+=("http://www.festvox.org/flite/packed/flite-2.1/voices/us_voices")

# Check if curl is installed
if [ -z "$(command -v curl)" ]; then
	echo "== CURL is needed to continue. Exiting..."
	exit 1
fi

# Function for downloading all files listed in the source file
download_voices_on_file() {
	parent_url=$(dirname "$1")
	curl -s -L "$1" -o /tmp/flite_tmp
	echo "== Downloaded $1..."

	# Loop through the source file line by line
	cat /tmp/flite_tmp | while read line
	do
		if [ -f "$line" ]; then
			echo "== $line exists, so skipping..."
		else
			echo "== Downloading $line..."
			# Download into a .part file for partial downloads
			curl -L "$parent_url/$line" -o "$line.part"
			# When download completes, rename .part file to original
			# name
			mv "$line.part" "$line"
		fi
	done
}

# Loop through source urls and process them
for i in "${sources[@]}"
do
	download_voices_on_file "$i"
done

# Delete temporary file
rm /tmp/flite_tmp

# Message that we're done
echo "== Done."
